Recoding

Quantitative Methodology (UPF)

Jordi Mas Elias

https://www.jordimas.cat/

Summary

  • Boolean operations
  • If_else
  • Case_when
  • Recode
  • As functions

Warm up

R learning curve

Hall of fame

Best plots!

RStudio workflow

Load packages.

library(dplyr)
library(ggplot2)
library(readr)
library(stringr)
library(forcats)
library(readxl)

Summary

Recoding

When we recode variables, we lose information1.

Destí Funció
Binària if_else()
Categòrica case_when()
Ordinal factor()
Qualsevol recode()
Altres as.numeric(), as.character(), as.Date(), etc.

Boolean operators

  • AND (&): TRUE if all conditions are met.
  • OR (|): TRUE if any condition is met.
  • NOT (!): TRUE if conditions are not met.

If_else

  • To a dichotomous / binary / dummy variable.
df |> 
  mutate(new_name = if_else(logic operation, true, false))

Case_when

case_when(logic operation ~ "C1"
          logic operation ~ "C2",
          logic operation ~ "C3",
          ...,
          TRUE ~ "CN")

Factor

df |> 
  mutate(new_vector = factor(wb$income_group, 
                             ordered = TRUE,
                             [levels o labels = ...]))

Recode

df |> 
  mutate(new_vector = recode(vector, 
                             old_value = "new_value"))

As functions

  • as.numeric(vector)
  • as.factor(vector)
  • as.character(vector)
  • as.integer(vector)
  • as.Date(vector)